package edu.nus.mtech.sem1.ca.main;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import edu.nus.mtech.sem1.ca.auth.User;
import edu.nus.mtech.sem1.ca.exception.NotAuthenticatedException;
import edu.nus.mtech.sem1.ca.utils.Common;

/**
 * Entry point of the application. 
 *
 */
public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	private JButton btnOk;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					Common.OpenCenterScreen(frame,false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
	
		setTitle("Telephony");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 343, 202);
		contentPane = new JPanel();
		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(54, 25, 79, 14);
		contentPane.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(54, 63, 77, 14);
		contentPane.add(lblPassword);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(141, 22, 155, 20);
		contentPane.add(txtUsername);
		txtUsername.setColumns(10);
		
		btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			@SuppressWarnings("unused")
			public void actionPerformed(ActionEvent arg0) {
					TryLogin();					
			}
		});
		btnOk.setBounds(67, 108, 89, 23);
		contentPane.add(btnOk);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(ABORT);
			}
		});
		btnCancel.setBounds(176, 108, 89, 23);
		contentPane.add(btnCancel);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(141, 60, 155, 20);
		
		txtPassword.addActionListener(new ActionListener(){
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TryLogin();
			}
			
		});
		contentPane.add(txtPassword);
	}
	private void TryLogin(){
		User l_user=new User();
		
		TelephoneApplication l_mainApp=new TelephoneApplication();
		try {
			l_user=l_mainApp.login(txtUsername.getText().trim(), new String(txtPassword.getPassword()));
			Main l_main=new Main();
			Common.OpenCenterScreen(l_main,true);
			setVisible(false);
		} catch (NotAuthenticatedException e) {			
			JOptionPane.showMessageDialog(null, "Incorrect username or password", "Warning", JOptionPane.ERROR_MESSAGE);
			System.out.print("Incorrect password");
			// TODO: handle exception for incorrect user name
		}
	}
}
